package framework.helpers;

import org.openqa.selenium.By;
import framework.ApplicationManager;
import framework.entities.AppointmentData;

public class ReferralsHelper extends BaseHelperForRmis
{
    public ReferralsHelper(ApplicationManager app) {
        super(app);
    }

	/**
	 * Создает случай госпитализации
	 *
	 * @param appointmentData данные для предварительной записи
	 */	
	public void createHospitalization(AppointmentData appointmentData) {
		log.info("Создание случая госпитализации:" + appointmentData);
		click(By.xpath("//a[contains(text(),'Действия')]"));
		waitWhileAjaxCompleted();
		click(By.xpath("(//a[contains(text(),'Оформить случай')])[2]"));
		waitWhileAjaxCompleted();

		chooseMedicalCaseType(appointmentData.medicalCaseType);
		chooseMedicalCaseFundingSourceType();
		chooseComplexityLevel();
		chooseMedicalInitGoal();
		chooseFirstHospitalRecordDepartment(appointmentData.firstHospitalRecordDepartment);
		chooseMedicalPaymentMethod();
		createDiagnoses();
		chooseFirstHospitalRecordResult();
		chooseSecondHospitalRecordDepartment(appointmentData.secondHospitalRecordDepartment);
		clickSaveButton();
	}
	
	private void chooseMedicalCaseType(String medicalCaseType) {
    	click(By.xpath("//*[@id='medicalCase.caseType.idContainer']/div/div"));
    	typeAndSelectByNumber(By.name("medicalCase.caseType.name"), medicalCaseType, 0);
	}
	
	private void chooseFirstHospitalRecordDepartment(String firstHospitalRecordDepartment) {
    	click(By.xpath("//*[@id='firstHospitalRecord.department.idContainer']/div/div"));
    	typeAndSelectByNumber(By.name("firstHospitalRecord.department.name"), firstHospitalRecordDepartment, 0);
	}
	
	private void createDiagnoses() {
		click(By.xpath("(//a[contains(text(),'Добавить')])[2]"));
		chooseFirstHospitalRecordCaseDiagnoses();
	}

	private void chooseFirstHospitalRecordCaseDiagnoses() {
    	click(By.xpath("//*[@id='firstHospitalRecord.caseDiagnoses$diagnos.idContainer']/div/div"));
    	typeAndSelectByNumber(By.name("_firstHospitalRecord.caseDiagnoses$diagnos.idEdit"), "", 0);
    	click(By.xpath("(//a[contains(text(),'Сохранить')])[5]"));
	}

	private void chooseFirstHospitalRecordResult() {
    	click(By.xpath("//*[@id='firstHospitalRecord.result.idContainer']/div/div"));
    	String result = "Перевод в другое отделение";
		typeAndSelectByNumber(By.name("firstHospitalRecord.result.name"), result, 0);
	}
	
	private void chooseSecondHospitalRecordDepartment(String secondHospitalRecordDepartment) {
    	click(By.xpath("//*[@id='secondHospitalRecord.department.idContainer']/div/div"));
    	typeAndSelectByNumber(By.name("secondHospitalRecord.department.name"), secondHospitalRecordDepartment, 0);
	}
	
	private void chooseMedicalCaseFundingSourceType() {
    	click(By.xpath("//*[@id='medicalCase.fundingSourceType.idContainer']/div/div"));
    	typeAndSelectByNumber(By.name("medicalCase.fundingSourceType.name"), "", 0);
	}
	
	private void chooseMedicalInitGoal() {
    	click(By.xpath("//*[@id='medicalCase.initGoal.idContainer']/div/div"));
    	typeAndSelectByNumber(By.name("medicalCase.initGoal.name"), "", 0);
	}

	private void chooseMedicalPaymentMethod() {
    	click(findElement(By.xpath("//*[@id='medicalCase.paymentMethod.idContainer']/div/div")));
    	typeAndSelectByNumber(By.name("medicalCase.paymentMethod.name"), "", 0);
	}
	
	private void chooseComplexityLevel() {
		click(By.cssSelector("#complexityLevel-to-show a"));
		click(By.xpath("//*[@id='medicalCase.complexityLevel.idContainer']/div/div"));
    	typeAndSelectByNumber(By.name("_medicalCase.complexityLevel.idEdit"), "", 0);
	}

}
